﻿using Styx.CommonBot.Coroutines;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.CommonBot;
using Styx.Common;
using System.Windows.Media;
using Styx.Pathing;

using spells = Paster.Spell.Spells;
using movement = Paster.Managers.Movement;
using groups = Paster.Managers.Groups;
using SD = Paster.GUI.DiscSettings;
using SC = Paster.GUI.CommonSettings;
using Movement = Paster.Managers.Movement;
using units = Paster.Managers.Units;

namespace Paster.Disc
{
    partial class Rotation
    {
        public static bool MeSolo { get { return groups.partyCount == 0; } }
        public static bool gotTarget { get { return Me.CurrentTarget != null && units.ValidUnit(Me.CurrentTarget); } }
        public static WoWGuid lstGuid;
        public static int dpsDist = units.dpsDistance - 5;
        public static DateTime _interTimer;
        public static bool _interTimerIsSet = false;
        public static int lastSpell = 0;

        public static async Task<bool> DiscRoutine()
        {
            if (Me.Mounted) { return false; }
            if (Main.pullTimer.IsRunning) { Main.pullTimer.Stop(); }
            if (Me.IsCasting || Me.IsChanneling) { return false; }

            if (await spells.CastBuff(spells.PowerWordShield, !units.buffExists(spells.PowerWordShield, Me)
                && Me.IsMoving
                && units.Canbuff
                && SpellManager.HasSpell("Body and Soul"), Me)) return true;

            if (SC.myPrefs.AutoMovement) await Movement.MoveToCombatTarget();
            if (SC.myPrefs.AutoFacing) await Movement.FaceMyCurrentTarget();

            if (await Movement.ClearMyDeadTarget()) return true;
            if (await units.ClearMyCurrentTarget()) return true;
            if (await Movement.ChangeTarget()) return true;
            
            if (await spells.Cast(spells.ArcaneTorrent, SpellManager.HasSpell(spells.ArcaneTorrent) 
                && !units.spellOnCooldown(spells.ArcaneTorrent)
                && Me.ManaPercent <= 85, Me)) return true;

            if (await spells.Cast(spells.DesperatePrayer, Me.HealthPercent <= SD.myPrefs.DesperatePrayerPrecent, Me)) return true;
            if (await spells.UseItem(healthStone, needHealthstone())) return true;

            

            if (await spells.CastBuff(spells.PowerWordFortitude, needFortitude(), Me)) return true;
            if (await spells.CastBuff(spells.PowerWordFortitude, needFortitudeAll(), Me)) return true;

            if (await spells.CastBuff(spells.FearWard, needFearWard(), Me)) return true;
            //if (await spells.Cast(spells.PsychicScream, units.specialAddCountUnits(8) >= SD.myPrefs.PsychicAddCount, Me)) return true;
            //if (await spells.Cast(spells.PsychicScream, units.specialAddCountPlayers(8) >= SD.myPrefs.PsychicPlayerCount, Me)) return true;
            if (await spells.Cast(spells.DispelMagic, dispelTargets != null, dispelTargets)) return true;
            if (await spells.Cast(spells.Purify, purifyPlayers != null, purifyPlayers)) return true;

            if (await CastTrinket(needTrinket1 && DateTime.Now >= _trinketTimer)) return true;
            if (await CastTrinket(needTrinket2 && DateTime.Now >= _trinketTimer)) return true;

            if (await spells.Cast(spells.PowerWordSolace, needPowerWordSolace(), dpsTarget)) return true;

            if (await spells.Cast(spells.PowerWordShield, KeepPwsOnTank() && tankPws != null, tankPws)) return true;
            if (await spells.Cast(spells.PowerWordShield, pwsUnit != null, pwsUnit)) return true;
            if (await spells.Cast(spells.Archangel, needArchangel(), Me)) return true;
            if (await spells.Cast(spells.PowerInfusion, needPowerInfusion(), Me)) return true;
            
            if (await spells.Cast(spells.Cascade, cascadeUnit != null , cascadeUnit)) return true;
            if (await spells.Cast(spells.PainSuppression, painUnit != null, painUnit)) return true;
            
            if (await spells.Cast(spells.Penance, penanceUnit != null, penanceUnit)) return true;
            if (await spells.Cast(spells.PrayerofHealing, pohUnit != null, pohUnit)) return true;
            if (await spells.Cast(spells.FlashHeal, flashUnit != null, flashUnit)) return true;
            if (await spells.Cast(spells.PrayerofMending, pomUnit != null, pomUnit)) return true;
            
            if (await spells.Cast(spells.Heal, healUnit != null, healUnit)) return true;
            if (await spells.Cast(spells.HolyNova, holyNovaUnit != null, Me)) return true;

            if (await spells.Cast(spells.ShadowWordPain, !MeSolo && dpsTarget != null && needAtonement() && !units.buffExists(spells.ShadowWordPain, dpsTarget), dpsTarget)) return true;
            if (await spells.Cast(spells.HolyFire, !MeSolo && dpsTarget != null && needAtonement(), dpsTarget)) return true;
            if (await spells.Cast(spells.Penance, !MeSolo && dpsTarget != null && needPenanceDps(), dpsTarget)) return true;
            if (await spells.Cast(spells.Smite, !MeSolo && dpsTarget != null && needAtonement(), dpsTarget)) return true;

            if (await spells.Cast(spells.ShadowWordPain, gotTarget && MeSolo &&  !units.buffExists(spells.ShadowWordPain, Me.CurrentTarget), Me.CurrentTarget)) return true;
            if (await spells.Cast(spells.HolyFire, gotTarget && MeSolo, Me.CurrentTarget)) return true;
            if (await spells.Cast(spells.Penance, gotTarget && MeSolo, Me.CurrentTarget)) return true;
            if (await spells.Cast(spells.Smite, gotTarget && MeSolo, Me.CurrentTarget)) return true;

            /*if (await units.CannotContinueFight(Me.CurrentTarget, Me.CurrentTarget != null
                && units.AutoBot
                && Me.CurrentTarget.HealthPercent >= 95
                && !Me.CurrentTarget.IsPlayer
                && Main.lastGuid == Me.CurrentTarget.Guid
                && Main.fightTimer.ElapsedMilliseconds >= 30 * 1000)) return true;*/
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }

        public static async Task<bool> CastInterrupt(string Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + Spell + " on: " + target.SafeName);
            _interTimerIsSet = false;
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
        public static async Task<bool> CastTrinket(bool reqs)
        {
            if (!reqs) return false;
            _trinketTimer = DateTime.Now + new TimeSpan(0,0,0,0,5000);
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
    }
}
